import React, { useEffect, useState } from 'react';
import { Button, DatePicker, message, Space } from 'antd';
import moment from 'moment';
import request from '../../../libs/request';
const SetTime = ({ setCurrent }) => {
  const [now, setNow] = useState(null);
  const [time, setTimer] = useState(null);
  useEffect(() => {

    request.get("/api/gslock/readtime").then(res => {

      setNow(moment(res.time))
    })


  }, []);


  const ok = () => {
    request.post("/api/v1/locker/sys-installs", {
      "install_key": "set_time",
      "install_step": 1
    }).then(res => {
      message.success("设置成功")
      setCurrent(1)
    })
  }

  return (
    <div style={{ textAlign: 'center' }}>
      <DatePicker
        showTime
        format="YYYY-MM-DD HH:mm:ss"
        value={now}                 // 受控，实时显示当前时间
        onChange={(value, dateString) => {
          console.log('Selected Time: ', value);
          console.log('Formatted Selected Time: ', dateString);
          setTimer(dateString);
          setNow(value)
        }}
        style={{ width: 300 }}
      />

      <Button
        type="primary"
        style={{ marginLeft: 10 }}
        onClick={() => {
          console.log('time', time)
          if (time) {
            request.post("/api/gslock/settime",{time}).then(res => {
              console.log(res)
              ok()

            })
          } else {
             ok()
          }



        }}
      >
        确定
      </Button>
    </div>
  );
};

export default SetTime;